package mws;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class 华为机试 {
    
    
    
    
    public static void main(String[] args) {
//
//        Scanner scanner = new Scanner(System.in);
//        Integer k = Integer.parseInt(scanner.nextLine());
//        Integer length = Integer.parseInt(scanner.nextLine());
//        int[] num = new int[length];
//
//
//        for (int i = 0; i < length; i++) {
//            num[i] = scanner.nextInt();
//        }

      new 华为机试().reverse(-123);




        // 测试用例1：小规模用例，正负得分混合
        int k1 = 2;
        int length1 = 3;
        int[] num1 = {1, -1, 2};
        List<Integer> list1 = new 华为机试().process(k1, length1, num1);
        System.out.println(list1);

// 测试用例2：小规模用例，需要跳过负值
        int k2 = 3;
        int length2 = 5;
        int[] num2 = {1, -2, 3, -4, 5};
        List<Integer> list2 = new 华为机试().process(k2, length2, num2);
        System.out.println(list2);

// 测试用例3：负得分为主，k=1
        int k3 = 1;
        int length3 = 3;
        int[] num3 = {-1, -2, -3};
        List<Integer> list3 = new 华为机试().process(k3, length3, num3);
        System.out.println(list3);

// 测试用例4：单个节点
        int k4 = 1;
        int length4 = 1;
        int[] num4 = {5};
        List<Integer> list4 = new 华为机试().process(k4, length4, num4);
        System.out.println(list4);

// 测试用例5：中等规模，交替正负得分
        int k5 = 2;
        int length5 = 6;
        int[] num5 = {1, -1, 2, -2, 3, -3};
        List<Integer> list5 = new 华为机试().process(k5, length5, num5);
        System.out.println(list5);

// 测试用例6：利用k跳过多个负值
        int k6 = 3;
        int length6 = 5;
        int[] num6 = {10, -1, -2, -3, 20};
        List<Integer> list6 = new 华为机试().process(k6, length6, num6);
        System.out.println(list6);

// 测试用例7：逐步跳跃优化得分
        int k7 = 2;
        int length7 = 10;
        int[] num7 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        List<Integer> list7 = new 华为机试().process(k7, length7, num7);
        System.out.println(list7);

// 测试用例8：交替正负得分，k=3
        int k8 = 3;
        int length8 = 10;
        int[] num8 = {10, -10, 10, -10, 10, -10, 10, -10, 10, -10};
        List<Integer> list8 = new 华为机试().process(k8, length8, num8);


        System.out.println(new 华为机试().processV2(1));


        System.out.println(new 华为机试().processV2(6));
        System.out.println(new 华为机试().processV3("aaazZ"));


    }
    
    
    
    public List<Integer> process(int k , int length, int[] num){


        ArrayList<Integer> list = new ArrayList<>();
        
        list.add(num[0]);
        int start = 0;
        for (int i = 0; i < length; i++) {
            int nextMax = Math.min(length - 1 ,start + k);
            int record = start;
            int max = Integer.MIN_VALUE;
            for (int j = start + 1; j <= nextMax && j < length; j++) {
                if(max <= num[j]){
                    max = num[j];
                    record = j;
                }
            }
            i = record;
            if(max != Integer.MIN_VALUE){
                list.add(max);
            }
            start = record;
        }
        return list;
    }
    
    
    
    
    public int  processV2(int n) {

       int[] dp = new int[n+1];
       dp[0] = 0;
       dp[1] = 5;
       int i = 1;
       while(n > dp[i]){
           i++;
           dp[i] = (((int) Math.pow(10,i) - (int)Math.pow(10,i-1)) / 2 * i) + dp[i-1];
       }

       if(i == 1) {
           return (n-1) * 2;
       }else {

           int i1 = n - dp[i];
           int i2 = n / i;

           double pow = Math.pow(10, i);
           int i3 = (int) (pow + i2 * 2);
           int i4 = n % i;
           char c = String.valueOf(i3).charAt(i4);
          return Integer.parseInt(String.valueOf(c));

       }

    }

    public int reverse(int x) {


        String reverseStr = String.valueOf(x);
        if(reverseStr.length() > 32)
            return 0;
        int cut = 0;
        if(x < 0){
            cut = 1;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = reverseStr.length() - 1; i >=  cut; i--) {
            stringBuilder.append(reverseStr.charAt(i));
        }
        String result = stringBuilder.toString();
        int i  = 0;
        int count = 0;
        while(count < result.length() - 1){
            char c = result.charAt(count);
            int re = c - '0';
            double pow = Math.pow(count, re);
            i += pow;
            count++;
        }
        char c = result.charAt(count);
        System.out.println(i);
        if(c == '0'){
            return i;
        }else {
            return -i;
        }
    }
   public String processV3(String string){


       byte[] bytes = string.getBytes();
       int length = bytes.length;

       int[] record = new int[length];
       for (int i = 0; i < length; i++) {
           if(bytes[i] >= 65 && bytes[i] < 97){
               record[i] = 1;
           }
       }
       StringBuilder sb = new StringBuilder();
       Character charAt = string.charAt(0);
       if(charAt >= 'A' && charAt <= 'Z'){
           charAt = String.valueOf(charAt).toLowerCase().charAt(0);
       }else if(charAt >= 'a' && charAt <= 'z'){
           charAt = String.valueOf(charAt).toUpperCase().charAt(0);
       }
       sb.append(charAt);
       for (int i = 1; i < string.length(); i++) {
           if(record[i-1] == 1 && record[i] == 0){
               Character charAt1 = string.charAt(i);
               if(charAt1 >= 'A' && charAt1 <= 'Z'){
                   charAt1 = String.valueOf(charAt1).toLowerCase().charAt(0);
               }else if(charAt1 >= 'a' && charAt1 <= 'z'){
                   charAt1 = String.valueOf(charAt1).toUpperCase().charAt(0);
               }
               sb.append(charAt1);
           }else if(record[i-1] == 0 && record[i] == 1){
               Character charAt1 = string.charAt(i);
               if(charAt1 >= 'A' && charAt1 <= 'Z'){
                   charAt1 = String.valueOf(charAt1).toLowerCase().charAt(0);
               }else if(charAt1 >= 'a' && charAt1 <= 'z'){
                   charAt1 = String.valueOf(charAt1).toUpperCase().charAt(0);
               }
               sb.append(charAt1);
           }else {
               Character charAt1 = string.charAt(i);

               if(charAt1 >= 'A' && charAt1 <= 'Z'){
                   sb.append((char)( (charAt1 - 'A' + (char)1) % 26 + 'A'));
               }else if(charAt1 >= 'a' && charAt1 <= 'z'){
                   sb.append((char)( (charAt1 - 'a' + (char)1) % 26 + 'a'));
               }

           }
       }
       return sb.toString();

   }
}
